#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLineEdit>
#include <QDateTime>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QLineEdit* edit = findChild<QLineEdit*>("lineEditUrl");
    if (edit)
    {
        //QString url = "rtsp://admin:sjld16@192.168.3.117/videoMain";
        QString url = "rtmp://live.hkstv.hk.lxdns.com/live/hks";
        edit->setText(url);
    }

    QFrame* frame = findChild<QFrame*>("frameVideo");
    HWND hwnd = (HWND)frame->winId();
	
    /// 使用SDL显示, windows下需要显示设置该属性, 其他平台不用
    av::LitePlayerFactory::setProp(MEDIA_VIDEO_PROP_RENDER, "sdl");

    m_player = av::LitePlayerFactory::create(hwnd);
    m_player->setEventSink(this);


    connect(this, &MainWindow::mediaEvent, this, &MainWindow::onPlayerMediaEvent);
}

MainWindow::~MainWindow()
{
    delete ui;

    av::LitePlayerFactory::destroy(m_player);
}

void MainWindow::on_pushButtonPlay_clicked()
{
    QString url = getMediaUrl();
    std::string mediaUrl = url.toStdString();

    /// 对于RTSP/RTP源, 使用RTP over TCP 的传输协议
    m_player->setProp(MEDIA_SOURCE_PROP_RTSP_TRANSPORT, "tcp");

    /// 设置媒体数据回调, 得到未解码的音视频包
    m_player->setPacketSink(this);

    m_player->open(mediaUrl.c_str());
    m_player->play();
}

void MainWindow::on_pushButtonPause_clicked()
{
    m_player->togglePlay();
}

void MainWindow::on_pushButtonStop_clicked()
{
    m_player->stop();
}

void MainWindow::on_pushButtonFull_clicked()
{
    m_player->toggleFull();
}

QString MainWindow::getMediaUrl()
{
    QLineEdit* edit = findChild<QLineEdit*>("lineEditUrl");
    if (edit)
    {
        return edit->text();
    }
    return "";
}

void MainWindow::on_pushButtonMute_clicked()
{
    m_player->toggleMute();
}

void MainWindow::on_pushButtonRecord_clicked()
{
    if (m_player->isRecording())
    {
        m_player->stopRecord();
    }
    else
    {
        QString filename = makeRecordName();
        std::string name = filename.toStdString();
        m_player->startRecord(name.c_str());
    }
}

QString MainWindow::makeRecordName()
{
    QDateTime dt = QDateTime::currentDateTime();
    QString timePart = dt.toString("yyyy-MM-dd-hhmmss");

    QString fileName = timePart + ".mp4";

    return fileName;
}

void MainWindow::onMediaEvent(av::LitePlayer* player, int type, int64_t value)
{
    emit mediaEvent(type);
}

void MainWindow::onPlayerMediaEvent(int type)
{
    QString text = QString("media event: %1").arg(type);
    if (type == av::MEDIA_EVENT_END)
    {
        text = "-- 流结束 --";

        /// 不要在回调中
    }
    else if (type == av::MEDIA_EVENT_FORMAT_READY)
    {
        av::MediaFormat fmt;
        m_player->getFormat(fmt);
        qDebug() << "width: " << fmt.m_width << " height: " << fmt.m_height;
    }

    QStatusBar* status = statusBar();
    status->showMessage(text);
}

void MainWindow::onMediaFormat(const av::MediaFormat& format)
{
    qDebug("onMediaFormat width:%d, height:%d, samplerate:%d, channels:%d",
           format.m_width, format.m_height, format.m_sampleRate, format.m_channels);
}

void MainWindow::onMediaPacket(MediaPacket& packet)
{
    //qDebug("onMediaPacket type:%d, size:%d", packet.type, packet.size);
}

void MainWindow::onMediaEnd()
{
    qDebug("onMediaEnd.");
}


void MainWindow::on_pushButtonSlow_clicked()
{
    m_player->slow();
}

void MainWindow::on_pushButtonFast_clicked()
{
    m_player->fast();
}

void MainWindow::on_pushButtonKeyFrame_clicked()
{
    m_player->nextKeyFrame();
}

void MainWindow::on_pushButtonStep_clicked()
{
    m_player->step();
}

void MainWindow::on_horizontalSlider_sliderReleased()
{

}
